package com.pig4cloud.pig.module.controller.platform;

import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.platform.PlatformSplit;
import com.pig4cloud.pig.module.service.platform.IPlatformSplitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 分成比例Controller
 *
 * @author ruoyi
 * @date 2021-08-17
 */
@Validated
@Api(value = "分成比例控制器", tags = {"分成比例管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/split")
public class PlatformSplitController {

    private final IPlatformSplitService platformSplitService;
//    private final ISysDictDataService sysDictDataService;

    /**
     * 查询分成比例列表
     */
    @ApiOperation("查询分成比例列表")
    @PreAuthorize("@pms.hasPermission('module:split:list')")
    @GetMapping("/list")
    public TableDataInfo<PlatformSplit> list(@Validated PlatformSplit platformSplit) {
        TableDataInfo<PlatformSplit> td = PageUtils.buildDataInfo(platformSplitService.page(PageUtils.buildPage()));
        List<PlatformSplit> list = td.getData();
        list.forEach(split->{
//			SysDictData dictData = sysDictDataService.selectDictData("split_type",split.getSplitType());
//			String content = "：业主：" + split.getProprietorSplit() + split.getSplitTypeUnit();
//			content += ", 物业：" + split.getPropertySplit() + split.getSplitTypeUnit();
//			content += "，平台：" + split.getPlatformSplit() + split.getSplitTypeUnit();
//			split.setSplitTypeName(dictData.getDictLabel() + content);
//			split.setSplitTypeUnit(dictData.getCssClass());

        });
        return td;
    }

    /**
     * 获取分成比例详细信息
     */
    @ApiOperation("获取分成比例详细信息")
    @PreAuthorize("@pms.hasPermission('module:split:query')")
    @GetMapping("/{id}")
    public R<PlatformSplit> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(platformSplitService.getById(id));
    }

    /**
     * 新增分成比例
     */
    @ApiOperation("新增分成比例")
    @PreAuthorize("@pms.hasPermission('module:split:add')")
    @SysLog("分成比例")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody PlatformSplit platformSplit) {
        return R.okByRow(platformSplitService.save(platformSplit) ? 1 : 0);
    }

    /**
     * 修改分成比例
     */
    @ApiOperation("修改分成比例")
    @PreAuthorize("@pms.hasPermission('module:split:edit')")
    @SysLog("分成比例")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody PlatformSplit platformSplit) {
        return R.okByRow(platformSplitService.updateById(platformSplit) ? 1 : 0);
    }

    /**
     * 删除分成比例
     */
    @ApiOperation("删除分成比例")
    @PreAuthorize("@pms.hasPermission('module:split:remove')")
    @SysLog("分成比例")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return R.okByRow(platformSplitService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
